package org.zjx.command.login;

import org.zjx.command.CompositeCommand;
import org.zjx.command.CompositeCommandBuilder;
import org.zjx.command.basic.MoveCommand;
import org.zjx.command.composite.FindImageCommand;
import org.zjx.core.IWindow;
import org.zjx.state.StateManager;

import java.awt.*;
import java.awt.image.BufferedImage;

public class LoginServerCommand extends CompositeCommand {
    public LoginServerCommand(IWindow window, StateManager stateManager) {
        super(window, stateManager);
    }

    @Override
    protected void initializeSubCommands() {
        CompositeCommandBuilder builder = new CompositeCommandBuilder(targetWindow, stateManager);

        CompositeCommand serverFlow = builder
                .find(LoginCommandConfig.Images.SERVER_PREVIOUS_IMAGE, LoginCommandConfig.Buttons.SERVER_PREVIOUS_BUTTON, "previousSureButtonPoint")
                .find(LoginCommandConfig.Images.SERVER_NEXT_IMAGE, LoginCommandConfig.Buttons.SERVER_NEXT_BUTTON, "nextButtonPoint")
                .find(LoginCommandConfig.Images.SERVER_AGREE_IMAGE, LoginCommandConfig.Buttons.SERVER_AGREE_BUTTON, "agreeButtonPoint")
                .ifFound("agreeButtonPoint", (b, context) -> {
                            Point point = (Point) context.get("agreeButtonPoint");
                            b.move(point.x, point.y)
                                    .delay(30)
                                    .click()
                                    .delay(1000);
                        }, (b, point) -> {
                            String[] main = stateManager.getAccountState().getMainLocation().split(",");
                            String[] sub = stateManager.getAccountState().getSubLocation().split(",");
                            b.move(Integer.parseInt(main[0]), Integer.parseInt(main[1]))
                                    .click()
                                    .delay(200)
                                    .move(Integer.parseInt(sub[0]), Integer.parseInt(sub[1]))
                                    .click()
                                    .delay(1000);
                        }
                )
                .build();

        add(new MoveCommand(targetWindow, 30, 30, stateManager));
        add(serverFlow);
    }

    public static boolean validate(IWindow window, BufferedImage screenshot) {
        FindImageCommand findPreviousImageCommand = new FindImageCommand(window, screenshot, LoginCommandConfig.Images.SERVER_PREVIOUS_IMAGE, LoginCommandConfig.Buttons.SERVER_PREVIOUS_BUTTON);
        findPreviousImageCommand.execute();
        FindImageCommand findNextImageCommand = new FindImageCommand(window, screenshot, LoginCommandConfig.Images.SERVER_NEXT_IMAGE, LoginCommandConfig.Buttons.SERVER_NEXT_BUTTON);
        findNextImageCommand.execute();
        FindImageCommand findAgreeImageCommand = new FindImageCommand(window, screenshot, LoginCommandConfig.Images.SERVER_AGREE_IMAGE, LoginCommandConfig.Buttons.SERVER_AGREE_BUTTON);
        findAgreeImageCommand.execute();
        return (findPreviousImageCommand.findImage() && (findNextImageCommand.findImage()) || (findAgreeImageCommand.findImage()));
    }
}
